//
//  z_DateTime.m
//  Template
//
//  Created by zhoubl on 13-6-3.
//  Copyright (c) 2013年 cdeledu. All rights reserved.
//

#import "z_DateTime.h"

@implementation z_DateTime

+ (NSString*) week
{
    // 周几和星期几获得
    NSDate*date = [NSDate date];
    
    NSCalendar*calendar = [NSCalendar currentCalendar];
    NSDateComponents * comps =[calendar components:(NSWeekCalendarUnit | NSWeekdayCalendarUnit |NSWeekdayOrdinalCalendarUnit)
            
                       fromDate:date];
    
    NSInteger weekday = [comps weekday]; // 星期几（注意，周日是“1”，周一是“2”。。。。）
    switch (weekday) {
        case 0:
            return @"周日";
            break;
        case 1:
            return @"周一";
            break;
        case 2:
            return @"周二";
            break;
        case 3:
            return @"周三";
            break;
        case 4:
            return @"周四";
            break;
        case 5:
            return @"周五";
            break;
        case 6:
            return @"周六";
            break;
            
        default:
            break;
    }
    
    return @"周一";
}

+(NSString *) day
{
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [calendar components:unitFlags fromDate:now];
    int day = [comps day];
    [calendar release];
    return [NSString stringWithFormat:@"%d",day];
}

+ (NSString *)nextMonthOfDate:(NSDate *)date forward:(BOOL)forward
{
    NSString *dateStr = nil;
    
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [calendar components:unitFlags fromDate:date];
    int year = [comps year];
    int month = [comps month];
    int day = [comps day];
    if (forward) {
        if (month+1>12) {
            year += 1;
            month = 1;
        }else{
            month += 1;
        }
    }else{
        if (month-1>0) {
            month -= 1;
        }else{
            year -= 1;
            month = 12;
        }
    }
    dateStr = [NSString stringWithFormat:@"%d-%02d-%02d",year,month,day];
    return dateStr;
}

+ (NSDate *)dateOffsetDays:(NSInteger)days fromDate:(NSDate *)date
{
    NSInteger oneDaySecs = 24*3600;
    NSTimeInterval now=[date timeIntervalSince1970]*1;
    NSTimeInterval late=oneDaySecs*days;
    NSTimeInterval cha=late+now;
    
    return [NSDate dateWithTimeIntervalSince1970:cha];
}

+ (NSInteger)indexOfWeekAtDate:(NSDate *)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents * comps =[calendar components:(NSWeekCalendarUnit | NSWeekdayCalendarUnit |NSWeekdayOrdinalCalendarUnit)
                               
                                          fromDate:date];
    
    return [comps weekday];
}

+ (BOOL)isSameOftheDate:(NSDate*)oneDate anotherDate:(NSDate*)anotherDate
{
    BOOL same = NO;
    
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *oneComps = [calendar components:unitFlags fromDate:oneDate];
    int oneYear = [oneComps year];
    int oneMonth = [oneComps month];
    int oneDay = [oneComps day];
    
    NSDateComponents *anotherComps = [calendar components:unitFlags fromDate:anotherDate];
    int anotherYear = [anotherComps year];
    int anotherMonth = [anotherComps month];
    int anotherDay = [anotherComps day];
    
    if (oneYear==anotherYear&&oneMonth==anotherMonth&&oneDay==anotherDay) {
        same = YES;
    }
    
    return same;
}

+ (BOOL)nowIsInFieldFrom:(NSDate*)fromDate to:(NSDate*)toDate
{
    BOOL flag = NO;
    
    int from = [self intervalSinceNow:[self encodeTime:fromDate]];
    int to = [self intervalSinceNow:[self encodeTime:toDate]];
    
    if (!(from>0||to<0)) {
        flag = YES;
    }
    
    return flag;
}

/**
 *	@brief	格式化时间为字符串
 *
 *	@param 	date 	NSDate系统时间类型
 *
 *	@return	返回默认格式yyyy-MM-dd HH:mm:ss
 */
+ (NSString *)encodeTime:(NSDate *)date

{
    @try {
        NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        return [formatter stringFromDate:date];
    }
    @catch (NSException *exception) {
        return @"";
    }
    @finally {
    }
}

/**
 *	@brief	字符串格式化为时间格式
 *
 *	@param 	dateString 	默认格式yyyy-MM-dd HH:mm:ss
 *
 *	@return	返回时间格式
 */
+ (NSDate *)dencodeTime:(NSString *)dateString

{
    @try {
        NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        return [formatter dateFromString:dateString];
    }
    @catch (NSException *exception) {
        return nil;
    }
    @finally {
    }
    
}
+ (NSDate *)dencodeTime2:(NSString *)dateString

{
    @try {
        NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        return [formatter dateFromString:dateString];
    }
    @catch (NSException *exception) {
        return nil;
    }
    @finally {
    }
    
}

/**
 *	@brief	离现在时间相差时间
 *
 *	@param 	date 	时间格式
 *
 *	@return	返回字符串
 */
+ (NSString *)timeSinceNow:(NSDate *)date

{
    @try {
        NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
        NSTimeInterval interval = 0 - [date timeIntervalSinceNow];
        
        if (interval < 60) {
            // 几秒前
            return @"1分钟内";
        } else if (interval < (60 * 60)) {
            // 几分钟前
            return [NSString stringWithFormat:@"%u分钟前", (int)(interval / 60)];
        } else if (interval < (24 * 60 * 60)) {
            // 几小时前
            return [NSString stringWithFormat:@"%u小时前", (int)(interval / 60 / 60)];
        } else if (interval < (2 * 24 * 60 * 60)) {
            // 昨天
            [formatter setDateFormat:@"昨天 HH:mm"];
            return [formatter stringFromDate:date];
        } else if (interval < (3 * 24 * 60 * 60)) {
            // 前天
            [formatter setDateFormat:@"前天 HH:mm"];
            return [formatter stringFromDate:date];
            //    } else if (interval < (7 * 24 * 60 * 60)) {
            // 一星期内
        } else {
            // 具体时间
            [formatter setDateFormat:@"yyyy/MM/dd HH:mm"];
            return [formatter stringFromDate:date];
        }
    }
    @catch (NSException *exception) {
        return @"";
    }
    @finally {
    }
}
+ (NSString *)timeSinceNow2:(NSDate *)date

{
    @try {
        NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
        //NSTimeInterval interval = 0 - [date timeIntervalSinceNow];
        
        // 具体时间
        [formatter setDateFormat:@"yyyy/MM/dd"];
        return [formatter stringFromDate:date];
        
    }
    @catch (NSException *exception) {
        return @"";
    }
    @finally {
    }
}

/**
 *	@brief	格式化时间为字符串
 *
 *	@param 	date 	时间
 *	@param 	format 	格式化字符串
 *
 *	@return	返回时间字符串
 */
+ (NSString *)encodeTime:(NSDate *)date format:(NSString *)format

{
    @try {
        NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
        [formatter setDateFormat:format];
        return [formatter stringFromDate:date];
    }
    @catch (NSException *exception) {
        return @"";
    }
    @finally {
    }
    
}

/**
 *	@brief  格式化成时间格式
 *
 *	@param 	dateString 	时间字符串
 *	@param 	format 	格式化字符串
 *
 *	@return	返回时间格式
 */
+ (NSDate *)dencodeTime:(NSString *)dateString format:(NSString *)format

{
    @try {
        NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
        [formatter setDateFormat:format];
        return [formatter dateFromString:dateString];
    }
    @catch (NSException *exception) {
        return nil;
    }
    @finally {
    }
    
}
/**
 *	判断时间大小 
 *
 *	@param	theDate	<#theDate description#>
 *
 *	@return	<#return value description#>
 */
+ (int)intervalSinceNow: (NSString *) theDate
{
    
    NSDate *  senddate=[NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *  locationString=[dateformatter stringFromDate:senddate];
    NSDate *nowdate=[dateformatter dateFromString:locationString];
    //NSLog(@"locadate=%@",locationString);
    
    NSDate *date=[dateformatter dateFromString:theDate];
    //    NSString *closedate1=[dateformatter stringFromDate:date];
    //    NSLog(@"closedate=%@",closedate1);
    
    [dateformatter release];
    
    
    NSTimeInterval late=[date timeIntervalSince1970]*1;//关课时间
    NSTimeInterval now=[nowdate timeIntervalSince1970]*1;//当前时间
    NSTimeInterval cha=late-now;
    NSInteger oneDaySecs = 24*3600;
    NSInteger twoDaySecs=cha/oneDaySecs;
    return twoDaySecs;
    
    //    如果 result＝０，　表示今天
    //    如果　result＝-1,　 表示昨天
    //    如果 result<-1  表示昨天以前
    //    如果　result>0   表示将来的某一天。
    
    
}

@end
